package tech.abing.spider.task.pageprocessor.sac;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.sac.SecComEntity;
import tech.abing.spider.task.model.sac.SecExecutiveEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class SecuritiesExecutiveProcessor implements PageProcessor {

	private static final String TOP_URL = "http://jg.sac.net.cn/pages/publicity/indicatorsFrame.html";
	
	private static Logger logger = Logger.getLogger(SecuritiesExecutiveProcessor.class);
	
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		String json = page.getContent();
		JSONArray jsonArray = JSONArray.fromObject(json);
		if(jsonArray.size()>0){
			JSONObject exeObj = jsonArray.getJSONObject(0);
			String aoiId = (String)page.getRequest().getExtParams().get("aoiId");
			logger.info("当前url："+TOP_URL+"?aoi_id="+aoiId+"&is_org_search=no");
			SecExecutiveEntity see = new SecExecutiveEntity();
			see.setAoiId(aoiId);
			see.setEiCurrentPosition(exeObj.getString("EI_CURRENT_POSITION"));
			see.setEiName(exeObj.getString("EI_NAME"));
			see.setEiOfficeDate(DateUtils.parseDate(exeObj.getString("EI_OFFICE_DATE")));
			see.setGcId(exeObj.getString("GC_ID"));
			see.setUrl(TOP_URL+"?aoi_id="+see.getAoiId()+"&is_org_search=no");
			see.setId(UuIdPublic.timeStrongID(5));
			StringUtils.removeSpace(see);
			objectContainer.add(see);
		}

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub

	}

}
